#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>


static int major;
static int minor;
module_param(major, int, S_IRUGO);
module_param(minor, int, S_IRUGO);

static dev_t dev_num;



static int __init dev_t_init(void)
{
    int ret;

    if (major)
    {
        dev_num = MKDEV(major, minor);
        printk(KERN_EMERG "major is : %d \r\n", major);
        printk(KERN_EMERG "minor is : %d \r\n", minor);
    
        ret = register_chrdev_region(dev_num, 1, "chrdev_name");
        if (ret < 0)
        {
            printk(KERN_EMERG "register chrdev error\r\n");
        }
        printk(KERN_EMERG "register chrdev ok\r\n");
    }
    else {
        ret = alloc_chrdev_region(&dev_num, 0, 1, "chrdev_name");
        if (ret < 0)   
        {
            printk(KERN_EMERG "alloc register chrdev error\r\n");
        } 
        printk(KERN_EMERG "alloc register chrdev ok\r\n");

        major = MAJOR(dev_num);
        minor = MINOR(dev_num);
        printk(KERN_EMERG "major is : %d \r\n", major);
        printk(KERN_EMERG "minor is : %d \r\n", minor);

    }
    return 0;
}

static void __exit dev_t_exit(void)
{
    unregister_chrdev_region(dev_num, 1);
    printk(KERN_EMERG "module exit");
}


module_init(dev_t_init);
module_exit(dev_t_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("treasure1029");



